#数据集划分
import os
import random

import shutil

# root_dir='./park_voc/VOC2007/'

class SplitClass(object):

    def __init__(self):
      print ("调用子类构造方法")

    def split(self,name):
        root_dir='E:/dataset/copy/'
        # root_dir='E:/dataset/copy/{}'.fromat(name)
        ## 0.7train 0.1val 0.2test
        trainval_percent = 0.8
        train_percent = 0.7
        xmlfilepath = root_dir+'image30_car/'
        # txtsavepath = root_dir+'main'
        # txtsavepath = root_dir+'ImageSets/Main'
        total_xml = os.listdir(xmlfilepath)

        num = len(total_xml)  # 100
        print('num',num )
        list = range(num)
        print('list ',list )
        tv = int(num*trainval_percent)  # 80
        tr = int(tv*train_percent)  # 80*0.7=56
        trainval = random.sample(list, tv)
        train = random.sample(trainval, tr)

        print(trainval,train)

        train_image_path='E:/dataset/copy/{}/images/'.format(name)
        train_label_path='E:/dataset/copy/{}/labels/'.format(name)

        val_image_path=''
        val_label_path=''

        if not os.path.exists(train_image_path):
            os.makedirs(train_image_path)
        if not os.path.exists(train_label_path):
            os.makedirs(train_label_path)

        # if not os.path.exists(train_image_path):
        #     os.makedirs(train_image_path)
        # if not os.path.exists(train_label_path):
        #     os.makedirs(train_label_path)

        for i in list:
            name0 = total_xml[i].split('.')[0]
            name = total_xml[i]
            print(name0,'------------------',name)

            if i in trainval:
                copyimagespath= train_image_path+name
                sourcepath=xmlfilepath+name
                shutil.copy(sourcepath,copyimagespath)


c=SplitClass()
c.split(1)


# c = Child()          # 实例化子类
# c.childMethod()      # 调用子类的方法
# c.parentMethod()     # 调用父类方法
# c.setAttr(200)       # 再次调用父类的方法 - 设置属性值
# c.getAttr()    

# root_dir='/dssg/home/zhineng_qt/test_2021_3_18_fu/dataset2/test_voc/small_voc/all/'


    # if i in trainval:
    #     ftrainval.write(name)
    #     if i in train:
    #         ftrain.write(name)
    #     else:
    #         fval.write(name)
    # else:
    #     ftest.write(name)

# # 检查文件夹是否存在
# def mkdir():
#     if not os.path.exists(train_image_path):
#         os.makedirs(train_image_path)
#     if not os.path.exists(train_label_path):
#         os.makedirs(train_label_path)
 
#     if not os.path.exists(test_image_path):
#         os.makedirs(test_image_path)
#     if not os.path.exists(test_label_path):
#         os.makedirs(test_label_path)
 
 
# def main():
#     mkdir()
#     # 复制移动图片数据
#     all_image = os.listdir(image_original_path)
#     for i in range(len(all_image)):
#         if i % 10 != 0:
#             copy2(os.path.join(image_original_path, all_image[i]), train_image_path)
#         else:
#             copy2(os.path.join(image_original_path, all_image[i]), test_image_path)
 
#     # 复制移动标注数据
#     all_label = os.listdir(label_original_path)
#     for i in range(len(all_label)):
#         if i % 10 != 0:
#             copy2(os.path.join(label_original_path, all_label[i]), train_label_path)
#         else:
#             copy2(os.path.join(label_original_path, all_label[i]), test_label_path)
 
 
# if __name__ == '__main__':
#     main()

# f_txt = open('/dssg/home/zhineng_qt/test_2021_3_18_fu/dataset2/test_voc/small_voc/all/main/train.txt', 'r')
# f_train = '/dssg/home/zhineng_qt/test_2021_3_18_fu/dataset2/test_voc/small_voc/all/getsplit/train'

# context = list(f_txt)
# for imagename in context:
#     imagename = imagename[0:6]
#     imagename = imagename + '.jpg'
#     imagepath = '/dssg/home/zhineng_qt/test_2021_3_18_fu/dataset2/test_voc/small_voc/all/images/'+ imagename
#     shutil.copy(imagepath,f_train)


# ftrainval = open(root_dir+'main/trainval.txt', 'w')
# ftest = open(root_dir+'main/test.txt', 'w')
# ftrain = open(root_dir+'main/train.txt', 'w')
# fval = open(root_dir+'main/val.txt', 'w')

# for i in list:
#     name = total_xml[i][:-4]+'\n'
#     if i in trainval:
#         ftrainval.write(name)
#         if i in train:
#             ftrain.write(name)
#         else:
#             fval.write(name)
#     else:
#         ftest.write(name)

# ftrainval.close()
# ftrain.close()
# fval.close()
# ftest .close()


# import shutil
# dir_path = r".\data"
# dst = r".\data_result" #这个目的文件是不存在的，copytree会自动创建
# shutil.copytree(dir_path,dst)
